package org.itsolutions.javaBackup.spi.backup.ftp;

import java.util.ArrayList;
import java.util.List;

import org.itsolutions.javaBackup.spi.backup.common.FileWrapper;

/**
 * 
 * @author Tomas Fecko
 */
public final class FTPFileWrapper extends FileWrapper {

	private static final long 	serialVersionUID = 7471363138226082081L;
	private String				mGroup;
	private String				mUser;
	private long				mGMT0Timestamp;
	private int					mHardLinkCount;
	private String				mLink;
	private String				mRawListing;
	private long				mSize;
	private EFileType			mFileType;
	private boolean[]			mPermissionScheme = new boolean[EFilePermision.values().length];
	private List<FTPFileWrapper>mListOfSubfiles = new ArrayList<FTPFileWrapper>();
	
	/**
	 * Add subfiles to list of subfiles. 
	 * @param pSubFiles subfiles
	 */
	public void addSubFiles(FTPFileWrapper... pSubFiles) {
		for (FTPFileWrapper subfile : pSubFiles) {
			mListOfSubfiles.add(subfile);
		}
	}
	
	/**
	 * Adds all the subfiles to list of subfiles.
	 * @param pSubfiles subfiles
	 */
	public void addSubFiles(List<FTPFileWrapper> pSubfiles) {
		mListOfSubfiles.addAll(mListOfSubfiles);
	}
	
	/**
	 * Clear the subfiles list.
	 */
	public void clearSubFiles() {
		mListOfSubfiles.clear();
	}
	
	/**
	 * @return the group
	 */
	public String getGroup() {
		return mGroup;
	}

	/**
	 * @param pGroup the group to set
	 */
	public void setGroup(String pGroup) {
		mGroup = pGroup;
	}

	/**
	 * @return the user
	 */
	public String getUser() {
		return mUser;
	}

	/**
	 * @param pUser the user to set
	 */
	public void setUser(String pUser) {
		mUser = pUser;
	}
	
	/**
	 * @return the gMT0Timestamp
	 */
	public long getGMT0Timestamp() {
		return mGMT0Timestamp;
	}

	/**
	 * @param pGMT0Timestamp the gMT0Timestamp to set
	 */
	public void setGMT0Timestamp(long pGMT0Timestamp) {
		mGMT0Timestamp = pGMT0Timestamp;
	}

	/**
	 * @return the hardLinkCount
	 */
	public int getHardLinkCount() {
		return mHardLinkCount;
	}

	/**
	 * @param pHardLinkCount the hardLinkCount to set
	 */
	public void setHardLinkCount(int pHardLinkCount) {
		mHardLinkCount = pHardLinkCount;
	}

	/**
	 * @return the lihnk
	 */
	public String getLink() {
		return mLink;
	}

	/**
	 * @param pLihnk the lihnk to set
	 */
	public void setLink(String pLihnk) {
		mLink = pLihnk;
	}

	/**
	 * @return the rawListing
	 */
	public String getRawListing() {
		return mRawListing;
	}

	/**
	 * @param pRawListing the rawListing to set
	 */
	public void setRawListing(String pRawListing) {
		mRawListing = pRawListing;
	}

	/**
	 * @return the size
	 */
	public long getSize() {
		return mSize;
	}

	/**
	 * @param pSize the size to set
	 */
	public void setSize(long pSize) {
		mSize = pSize;
	}

	/**
	 * @return the fileType
	 */
	public EFileType getFileType() {
		return mFileType;
	}

	/**
	 * @param pFileType the fileType to set
	 */
	public void setFileType(EFileType pFileType) {
		mFileType = pFileType;
	}

	/**
	 * 
	 * @return True if the file is of type <code>DIRECTORY_TYPE</code>, false if not.
	 */
    public boolean isDirectory() {
        return mFileType == EFileType.DIRECTORY_TYPE;
    }

    /***
     * Determine if the file is a regular file.
     * @return True if the file is of type <code>FILE_TYPE</code>, false if not.
     */
    public boolean isFile() {
        return mFileType == EFileType.FILE_TYPE;
    }

    /***
     * Determine if the file is a symbolic link.
     * @return True if the file is symbolic link.
     */
    public boolean isSymbolicLink() {
        return mFileType == EFileType.SYMBOLIC_LINK_TYPE;
    }

    /**
     * 
     * @return true, if file type is unknown.
     */
    public boolean isUnknown() {
        return mFileType == EFileType.UNKNOWN_TYPE;
    }	

    /**
     * 
     * @param pPermission permission
     */
    public void addPermission(EFilePermision pPermission) {
    	if (pPermission != null) {
    		mPermissionScheme[pPermission.ordinal()] = true;
    	}
    }
    
    /**
     * 
     * @param pPermission permission
     * @return tru, if file has this permission
     */
    public boolean hasPermission(EFilePermision pPermission) {
    	if (pPermission != null) {
    		return mPermissionScheme[pPermission.ordinal()];
    	} else {
    		return false;
    	}
    }

	/**
	 * Enum for file type. 
	 */
	public static enum EFileType {
		/**
		 * File.
		 */
		FILE_TYPE,
		/**
		 * Directory.
		 */
		DIRECTORY_TYPE,
		/**
		 * Symbolic link.
		 */
		SYMBOLIC_LINK_TYPE,
		/**
		 * Unknown.
		 */
		UNKNOWN_TYPE;
	}
	
	/**
	 * Enum for file access and permisions. 
	 */
	public static enum EFilePermision {
		//CHECKSTYLE:OFF
		USER_CAN_READ,
		USER_CAN_WRITE,
		USER_CAN_EXECUTE,
		GROUP_CAN_READ,
		GROUP_CAN_WRITE,
		GROUP_CAN_EXECUTE,		
		WORLD_CAN_READ,
		WORLD_CAN_WRITE,
		WORLD_CAN_EXECUTE;
		//CHECKSTYLE:ON		
	}
}




